package com.fengye.school.service;

import com.fengye.school.model.school.Teacher;
import com.fengye.school.response.ResponsePageResult;
import com.fengye.school.respository.TeacherRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class TeacherService {

    @Resource
    private TeacherRepository teacherRepository;

    public Teacher save(Teacher teacher){
        Teacher save = teacherRepository.save(teacher);
        log.info("保存成功, teacher={}", save);
        return save;
    }


    public List<Teacher> list() {
        return teacherRepository.findAll();
    }

    public ResponsePageResult<Teacher> listByPage(int page, int size) {
        Pageable pageRequest = PageRequest.of(page, size, Sort.Direction.DESC, "createTime");
        Page<Teacher> all = teacherRepository.findAll(pageRequest);
        return ResponsePageResult.success("获取所有教师信息成功", all.getTotalElements(), all.getContent());
    }

    public void deleteAll(Long[] ids) {
        Arrays.stream(ids).forEach(id -> teacherRepository.deleteById(id));
    }

    public void deleteById(Long id) {
        teacherRepository.deleteById(id);
    }
}
